package com.groupten.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.groupten.Entity.StudentEntity;
import com.groupten.Entity.TeacherEntity;
import com.groupten.Service.LoginService;
import com.groupten.Service.StudentService;
import com.groupten.Service.TeacherService;
import com.groupten.Utility.PasswordUtils;
import com.groupten.Vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("loginService")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Override
    public String login(String number, String password) {
        TeacherEntity teacher = teacherService.selectByNumber(number);
        if (teacher != null) {
            if (PasswordUtils.check(password, teacher.getTeacherPassword())) {
                return "Login successfully : Teacher " + teacher.getTeacherName();
            }else {
                return "Password error";
            }
        }
        StudentEntity student = studentService.selectByNumber(number);
        if (student != null) {
            if (PasswordUtils.check(password, student.getStudentPassword())) {
                return "Login successfully : Student " + student.getStudentName();
            }else {
                return "Password error";
            }
        }
        return "No such user";
    }

    @Override
    public String login(LoginVo loginVo) {
        String number = loginVo.getNumber();
        String password = loginVo.getPassword();
        // Try to find student
        StudentEntity student = studentService.selectByNumber(number);
        if (student != null && PasswordUtils.check(password, student.getStudentPassword())) {
            return "Login successful: Student";
        }
        // Try to find teacher
        TeacherEntity teacher = teacherService.selectByNumber(number);
        if (teacher != null && PasswordUtils.check(password, teacher.getTeacherPassword())) {
            return "Login successful: Teacher";
        }
        return "Login failed:Incorrect number or password";

    }

    @Override
    public String registerOfStudent(StudentEntity student) {
        if (studentService.selectByNumber(student.getStudentNumber()) != null) {
            return "Student already exists";
        } else {
            String passwordRegex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!%*?&.])[A-Za-z\\d@$!%*?&.]{8,}$";
            if (!student.getStudentPassword().matches(passwordRegex)) {
                return "Password must contain at least one uppercase letter, one lowercase letter, one digit, and one special character";
            }
            studentService.insert(student);
            return "Register successfully";
        }
    }

    @Override
    public String registerOfTeacher(TeacherEntity teacher) {
        if (teacherService.selectByNumber(teacher.getTeacherNumber()) != null) {
            return "Teacher already exists";
        } else {
            String passwordRegex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!%*?&.])[A-Za-z\\d@$!%*?&.]{8,}$";
            if (!teacher.getTeacherPassword().matches(passwordRegex)) {
                return "Password must contain at least one uppercase letter, one lowercase letter, one digit, and one special character";
            }
            teacherService.insertTeacher(teacher);
            return "Register successfully";
        }
    }

    @Override
    public Boolean encryptPasswordOfStudent(String studentNumber) {
        StudentEntity student = studentService.selectByNumber(studentNumber);
        if (student != null) {
            String encryptedPassword = PasswordUtils.encrypt(student.getStudentPassword());
            student.setStudentPassword(encryptedPassword);
            studentService.updateById(student);
            return true;
        } else {
            throw new NullPointerException("No such student");
        }
    }

    @Override
    public Boolean encryptPasswordOfTeacher(String teacherNumber) {
        TeacherEntity teacher = teacherService.selectByNumber(teacherNumber);
        if (teacher != null) {
            String encryptedPassword = PasswordUtils.encrypt(teacher.getTeacherPassword());
            teacher.setTeacherPassword(encryptedPassword);
            teacherService.updateById(teacher);
            return true;
        } else {
            throw new NullPointerException("No such teacher");
        }
    }

}